//
//  VRCaipiaoDetailViewController.m
//  VRKaiCai
//
//  Created by slh on 2017/6/23.
//  Copyright © 2017年 slh. All rights reserved.
//

#import "VRCaipiaoDetailViewController.h"

#import "VRCaipiaoDetailCell.h"
#import "VRCaipiaoOpen.h"

@interface VRCaipiaoDetailViewController ()<UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate>


@property (nonatomic, weak) UITableView *tableView;

@property (nonatomic, weak) UIView *headerView;
@property (nonatomic, weak) UILabel *issuenoLbl;
@property (nonatomic, weak) UILabel *numberLbl;
@property (nonatomic, weak) UILabel *opendateLbl;
@property (nonatomic, weak) UILabel *deadlineLbl;
@property (nonatomic, weak) UILabel *saleamountLbl;
@property (nonatomic, weak) UILabel *totalmoneyLbl;


@property (nonatomic, strong) NSMutableArray *detailModelArray;

@end

@implementation VRCaipiaoDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"开奖信息";
    self.view.backgroundColor = [UIColor colorWithWhite:0.965 alpha:1];
    
    self.detailModelArray = [NSMutableArray array];
    
    [self setupSubView];
    
    [self getCaipiaoDetail];
}


- (void)setupSubView
{
    /** 
     issueno:期号
     number:开奖号码
     opendate:开奖日期
     deadline：兑奖截止日期
     saleamount：销售额
     totalmoney：奖池奖金
     */
    UILabel *tipLbl = [[UILabel alloc] init];
    tipLbl.text = @"还没有开奖信息哦";
    tipLbl.font = [UIFont systemFontOfSize:16];
    //    issuenoLbl.textColor = VRColor(146, 146, 146);
    [self.view addSubview:tipLbl];
    [tipLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(120);
    }];
    
    UIView *headerView = [[UIView alloc] init];
    headerView.hidden = YES;
    headerView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:headerView];
    self.headerView = headerView;
    [headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.view);
        make.top.mas_equalTo(self.view).offset(64);
        make.width.mas_equalTo(Screen_width);
        make.height.mas_equalTo(120);
    }];
    
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = [UIColor lightGrayColor];
    [headerView addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(headerView);
        make.bottom.mas_equalTo(headerView).offset(0);
        make.width.mas_equalTo(Screen_width);
        make.height.mas_equalTo(0.5);
    }];
    
    UILabel *issuenoLbl = [[UILabel alloc] init];
    issuenoLbl.text = @"期号:";
    issuenoLbl.font = [UIFont systemFontOfSize:16];
//    issuenoLbl.textColor = VRColor(146, 146, 146);
    [headerView addSubview:issuenoLbl];
    self.issuenoLbl = issuenoLbl;
    [issuenoLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.top.mas_equalTo(15);
    }];
    
    UILabel *numberLbl = [[UILabel alloc] init];
    numberLbl.text = @"开奖号码:";
    numberLbl.font = [UIFont systemFontOfSize:16];
    //    issuenoLbl.textColor = VRColor(146, 146, 146);
    [headerView addSubview:numberLbl];
    self.numberLbl = numberLbl;
    [numberLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(issuenoLbl);
        make.top.mas_equalTo(issuenoLbl.mas_bottom).offset(5);
    }];
    
    UILabel *opendateLbl = [[UILabel alloc] init];
    opendateLbl.text = @"开奖日期:";
    opendateLbl.font = [UIFont systemFontOfSize:14];
    //    issuenoLbl.textColor = VRColor(146, 146, 146);
    [headerView addSubview:opendateLbl];
    self.opendateLbl = opendateLbl;
    [opendateLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(issuenoLbl);
        make.top.mas_equalTo(numberLbl.mas_bottom).offset(5);
    }];
    
    
    UILabel *deadlineLbl = [[UILabel alloc] init];
    deadlineLbl.text = @"截止日期:";
    deadlineLbl.font = [UIFont systemFontOfSize:14];
    //    issuenoLbl.textColor = VRColor(146, 146, 146);
    [headerView addSubview:deadlineLbl];
    self.deadlineLbl = deadlineLbl;
    [deadlineLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(headerView.mas_centerX).offset(10);
        make.top.mas_equalTo(opendateLbl);
    }];
    
    UILabel *saleamountLbl = [[UILabel alloc] init];
    saleamountLbl.text = @"销售额:";
    saleamountLbl.font = [UIFont systemFontOfSize:14];
    //    issuenoLbl.textColor = VRColor(146, 146, 146);
    [headerView addSubview:saleamountLbl];
    self.saleamountLbl = saleamountLbl;
    [saleamountLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(issuenoLbl);
        make.top.mas_equalTo(opendateLbl.mas_bottom).offset(5);
    }];
    
    UILabel *totalmoneyLbl = [[UILabel alloc] init];
    totalmoneyLbl.text = @"奖池奖金:";
    totalmoneyLbl.font = [UIFont systemFontOfSize:14];
    //    issuenoLbl.textColor = VRColor(146, 146, 146);
    [headerView addSubview:totalmoneyLbl];
    self.totalmoneyLbl = totalmoneyLbl;
    [totalmoneyLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(headerView.mas_centerX).offset(10);
        make.top.mas_equalTo(saleamountLbl);
    }];

//    [headerView mas_updateConstraints:^(MASConstraintMaker *make) {
//        make.bottom.mas_equalTo(totalmoneyLbl.mas_bottom).offset(10);
//    }];

    
    
    UITableView *tableView = [[UITableView alloc] init];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.tableFooterView = [[UIView alloc] init];
    [self.view addSubview:tableView];
    self.tableView = tableView;
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(self.view);
        make.top.mas_equalTo(headerView.mas_bottom);
    }];
}


#pragma mark - data
// 获取彩票类别
- (void)getCaipiaoDetail
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [[NSSet  alloc] initWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil];
    //    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    manager.requestSerializer=[AFJSONRequestSerializer serializer];
    
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"appkey"] = @"94cd7eb2aeb10bda";
    param[@"caipiaoid"] = @(self.caipiaoid);

    
    [manager GET:URL_CaiPiaoQuery parameters:param progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        VRLog(@"result:%@", responseObject);
        
        NSDictionary *resultDict = responseObject[@"result"];
        if (resultDict == nil || ![resultDict isKindOfClass:[NSDictionary class]]) {
            
            self.headerView.hidden = YES;
            return ;
        }else if([resultDict isKindOfClass:[NSDictionary class]] && ![resultDict[@"prize"] isKindOfClass:[NSArray class]]){
            
            self.headerView.hidden = NO;
            self.issuenoLbl.text = [NSString stringWithFormat:@"%@%@", self.issuenoLbl.text, resultDict[@"issueno"]];
            self.numberLbl.text = [NSString stringWithFormat:@"%@%@", self.numberLbl.text, resultDict[@"number"]];
            self.opendateLbl.text = [NSString stringWithFormat:@"%@%@", self.opendateLbl.text, resultDict[@"opendate"]];
            self.deadlineLbl.text = [NSString stringWithFormat:@"%@%@", self.deadlineLbl.text, resultDict[@"deadline"]];
            self.saleamountLbl.text = [NSString stringWithFormat:@"%@%@", self.saleamountLbl.text, resultDict[@"saleamount"]];
            self.totalmoneyLbl.text = [NSString stringWithFormat:@"%@%@", self.totalmoneyLbl.text, resultDict[@"totalmoney"]];
            
            VRCaipiaoOpen *item = [[VRCaipiaoOpen alloc] init];
            [item setValuesForKeysWithDictionary:resultDict];
            
            [self.detailModelArray addObject:item];
        }else{
            
            self.headerView.hidden = NO;
            self.issuenoLbl.text = [NSString stringWithFormat:@"%@%@", self.issuenoLbl.text, resultDict[@"issueno"]];
            self.numberLbl.text = [NSString stringWithFormat:@"%@%@", self.numberLbl.text, resultDict[@"number"]];
            self.opendateLbl.text = [NSString stringWithFormat:@"%@%@", self.opendateLbl.text, resultDict[@"opendate"]];
            self.deadlineLbl.text = [NSString stringWithFormat:@"%@%@", self.deadlineLbl.text, resultDict[@"deadline"]];
            self.saleamountLbl.text = [NSString stringWithFormat:@"%@%@", self.saleamountLbl.text, resultDict[@"saleamount"]];
            self.totalmoneyLbl.text = [NSString stringWithFormat:@"%@%@", self.totalmoneyLbl.text, resultDict[@"totalmoney"]];
            
            NSArray *array = resultDict[@"prize"];
            
            for (NSDictionary *obj in array) {
                
                VRCaipiaoOpen *item = [[VRCaipiaoOpen alloc] init];
                [item setValuesForKeysWithDictionary:obj];
                
                [self.detailModelArray addObject:item];
            }
        }
        [self.tableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        VRLog(@"erro:%@", error);
    }];
}


#pragma mark - delegate
#pragma mark TableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 130;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.detailModelArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    VRCaipiaoOpen *model = self.detailModelArray[indexPath.row];
    
    VRCaipiaoDetailCell *cell = [VRCaipiaoDetailCell cellWith:tableView];
    cell.model = model;
    
    
    return cell;
}


@end
